package com.example.tvrecyclerview.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentManager;

import com.example.tvrecyclerview.R;
import com.example.tvrecyclerview.util.Constants;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author: njb
 * @date: 2020/9/24 0024 1:03
 * @desc:
 */

abstract class FragmentMenuBase extends FragmentBase {
    private String childTitle;

    @Nullable
    protected String getTitle() {
        return getTitle(2, true);
    }

    protected final String getCurrentTitle() {
        Bundle bundle = getArguments();
        return (bundle != null ? bundle.getString(Constants.key.childTitle, Constants.EMPTY_STRING) : Constants.EMPTY_STRING);
    }




    protected boolean isReadyPlayShown() {
        return false;
    }

    protected boolean isMarqueeShown() {
        return false;
    }

    protected boolean isInfoBottomShown() {
        return false;
    }

    protected boolean isDigitalSongEnable() {
        return true;
    }

    protected final void setChildTitle(@Nullable String childTitle) {
        this.childTitle = childTitle;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void afterInitView() {
        super.afterInitView();


    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
    }

    @Override
    protected final void showFragment(FragmentManager fragmentManager, FragmentBase fragment, boolean addToBackStack) {
        Bundle bundle = fragment.getArguments();
        if (bundle == null) {
            bundle = new Bundle();
        }
        ArrayList<String> parentTitle = new ArrayList<>();
        parentTitle.addAll(getParentTitle());
        parentTitle.add(getCurrentTitle());
        bundle.putStringArrayList(Constants.key.parentTitle, parentTitle);
        bundle.putString(Constants.key.childTitle, childTitle != null ? childTitle : Constants.EMPTY_STRING);
        fragment.setArguments(bundle);
        super.showFragment(fragmentManager, fragment, addToBackStack);
    }

    private List<String> getParentTitle() {
        ArrayList<String> parentTitle = null;
        Bundle bundle = getArguments();
        if (bundle != null) {
            parentTitle = bundle.getStringArrayList(Constants.key.parentTitle);
        }
        if (parentTitle == null) {
            parentTitle = new ArrayList<>();
            parentTitle.add(Constants.EMPTY_STRING);
        }
        return Collections.unmodifiableList(parentTitle);
    }

    /**
     * @param level title levels, if the level <=0, will return a full title
     * @param last  the first level titles or the last titles
     * @return the full title match the given rule and join with link
     */
    private String getTitle(int level, boolean last) {
        String link = getString(R.string.link);
        String current = getCurrentTitle();
        List<String> parent = getParentTitle();
        ArrayList<String> temp = new ArrayList<>();
        if (level <= 0) {
            level = parent.size();
            last = false;
        }
        if (last) {
            temp.add(current);
            --level;
            for (int i = parent.size() - 1; level > 0 && i >= 0; i--) {
                String s = parent.get(i);
                if (!s.isEmpty()) {
                    temp.add(0, s);
                    if (--level == 0) {
                        break;
                    }
                }
            }
        } else {
            for (String s : parent) {
                if (!s.isEmpty()) {
                    temp.add(s);
                    if (--level == 0) {
                        break;
                    }
                }
            }
            if (level > 0) {
                temp.add(current);
            }
        }
        return Constants.join(temp, link);
    }





    /**
     * {@link MainActivity#updateInfoBottom(int, int)}
     */
    protected final void updateInfoBottom(int index, int state) {
//        getSongBook().updateInfoBottom(index, state);
    }

    protected void onUpdateInfoBottom() {
        /*fix mantis 0013041*/
        updateInfoBottom(0, 0x000);
        updateInfoBottom(1, 0x000);
        updateInfoBottom(2, 0x000);
        updateInfoBottom(3, 0x000);
        updateInfoBottom(4, 0x000);
        updateInfoBottom(5, 0x000);
        updateInfoBottom(6, 0x000);
        updateInfoBottom(7, 0x000);
        updateInfoBottom(8, 0x000);
        updateInfoBottom(9, 0x011);
    }

}
